package SA.teacher.daily;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import SA.teacher.*;
import SA.teacher.answer.AnswerActivity1;
import SA.teacher.manage.ManageActivity1;
import SA.teacher.notice.NoticeActivity1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class DailyToday2 extends Activity {

	String dailyInfoStr;

	public static String content;
	public static String classid;
	public static String tid;
	public static String adate;
	String myResult;
	public static String id;

	String rcvclassid;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.daily2_today);

		//////////////Date and class Info TextView//////////////////////
		Intent intent = getIntent();

		dailyInfoStr = intent.getStringExtra("dailyInfo");

		id = intent.getStringExtra("ID");
		String[] dateclass = dailyInfoStr.split(",");
		adate = dateclass[0];
		classid = dateclass[1];
		dailyInfoStr = dailyInfoStr +"占쏙옙";
		TextView dailyInfo = (TextView) findViewById(R.id.dailyInfo);
		dailyInfo.setText(dailyInfoStr);

		///////////////Save Button//////////////////////////////////////
		ImageView saveBtn = (ImageView) findViewById(R.id.savebtn);
		saveBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				EditText todayetext = (EditText)findViewById(R.id.todayetext);			

				tid = "1234";
				content = todayetext.getText().toString();			
				daysave(content);			
			}
		});

		/////////////Round Button action//////////////////////////////////////
		ImageView dailySpecial = (ImageView) findViewById(R.id.dailyspecial);
		dailySpecial.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dailySpecial();
			}
		});
		ImageView dailyNext = (ImageView) findViewById(R.id.dailynext);
		dailyNext.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dailyNext();	
			}
		});
		ImageView dailyHW = (ImageView) findViewById(R.id.dailyhw);
		dailyHW.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dailyHW();
			}
		});

		//////////////Button action////////////////////////////////////
		TextView noticeBtn = (TextView) findViewById(R.id.notice);
		noticeBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				noticeActivity();
			}
		});

		ImageView mainBtn = (ImageView) findViewById(R.id.mainBtn);
		mainBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mainActivity();
			}
		});

		ImageView manageBtn = (ImageView) findViewById(R.id.manageBtn);
		manageBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				manageActivity();
			}
		});

		ImageView dailyBtn = (ImageView) findViewById(R.id.dailyBtn);
		dailyBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dailyActivity();
			}
		});
		ImageView answerBtn = (ImageView) findViewById(R.id.answerBtn);
		answerBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				answerActivity();	
			}
		});
	}
	//TODO sdagregmopweaj'g

	void daysave (String content) {
		// 占승다몌옙 占싸깍옙占쏙옙, 틀占쏙옙占쏙옙 창占쏙옙占쏙옙
		Log.e("login 占쏙옙占쏙옙 : ", "1");
		//HttpPostData();
		dayParse ps = new dayParse();
		Log.e("login 占쏙옙占쏙옙 : ", "2");
		ps.run();

		Log.e("login 占쏙옙占쏙옙 : ", "3");
		Log.e("占쏙옙占쏙옙 : ", content);
		Log.e("login 占쏙옙占쏙옙 : ", "4");


	}

	//////////////Round Button//////////////////////////////////////
	void dailySpecial(){
		Intent intent = new Intent(this, DailySpecial3.class);

		intent.putExtra("ID",id);
		intent.putExtra("dailyInfo",dailyInfoStr);
		startActivity(intent);
	}
	void dailyNext(){
		Intent intent = new Intent(this, DailyNext4.class);

		intent.putExtra("ID",id);
		intent.putExtra("dailyInfo",dailyInfoStr);
		startActivity(intent);
	}
	void dailyHW(){
		Intent intent = new Intent(this, DailyHW5.class);
		intent.putExtra("ID",id);
		intent.putExtra("dailyInfo",dailyInfoStr);
		startActivity(intent);
	}

	public class classidparse implements Runnable {
		public void run() {

			// TODO Auto-generated method stub
			try {
				URL url = new URL("Http://220.67.128.8/dayclassid.php?id=" + URLEncoder.encode(id, "EUC-KR"));
			
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();

				InputStream  is   = null;
				ByteArrayOutputStream baos = null; 
				if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
					is = conn.getInputStream();  
					baos = new ByteArrayOutputStream();
					byte[] byteBuffer = new byte[1024];    byte[] byteData = null; 
					int nLength = 0;
					while((nLength = is.read(byteBuffer)) > 0) {   
						baos.write(byteBuffer, 0, nLength); 
					}
					byteData = baos.toByteArray(); 
					String response = new String(byteData, "euc-kr");   
					if(response==null || response.equals("")) {    
						throw new Exception("Response is NULL!");  
					}
					rcvclassid =response;
				}
				else{
					Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙 占싫듸옙");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}
	}

	private class dayParse implements Runnable {
		public void run() {
			// TODO Auto-generated method stub
			try {
				//String addr ="Http://220.67.128.8/temp.php?useUnicode=true&characterEncoding=EUC-KR&tID=1234" + "&adate=20120902" +  "&classid="+  classid + "&content="+content;


				URL url = new URL("Http://220.67.128.8/temp.php?tID=" +URLEncoder.encode(tid,"EUC-KR")
						+ "&adate=" +URLEncoder.encode(adate,"EUC-KR")
						+  "&classid="+URLEncoder.encode(classid,"EUC-KR")
						+ "&content="+URLEncoder.encode(content,"EUC-KR"));

				Log.e("1", tid);
				Log.e("2", adate);
				Log.e("3", classid);
				Log.e("4", content);
				//url.openStream();
				//URL url = new URL(addr);

				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();

				InputStream  is   = null;
				ByteArrayOutputStream baos = null; 
				if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
					is = conn.getInputStream();  
					baos = new ByteArrayOutputStream();
					byte[] byteBuffer = new byte[1024];    byte[] byteData = null; 
					int nLength = 0;
					while((nLength = is.read(byteBuffer)) > 0) {   
						baos.write(byteBuffer, 0, nLength); 
					}
					byteData = baos.toByteArray(); 
					String response = new String(byteData, "euc-k-r"); 
					if(response==null || response.equals("")) {    
						throw new Exception("Response is NULL!");  
					}
				}
				else{
					Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙 占싫듸옙");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}
	}
	
	//////////////Button �⑥닔��///////////////////////////////////
	void noticeActivity() {
		Intent intent = new Intent(this, NoticeActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void mainActivity() {
		Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void manageActivity() {
		classidparse cidps = new classidparse();
		cidps.run();
		final String[] classes = rcvclassid.split(",");
		if(classes.length-1!=0){
			new AlertDialog.Builder(DailyToday2.this)
			.setTitle("諛섏쓣 �좏깮�댁＜�몄슂.")
			.setItems(classes, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int which) {
					Intent intent = new Intent(DailyToday2.this, ManageActivity1.class);
					intent.putExtra("ID",id);
					intent.putExtra("classname",classes[which]);
					startActivity(intent);
				}
			}).show();
		}
		else{
			new AlertDialog.Builder(DailyToday2.this)
			.setMessage("�꾩옱 �대떦�섎뒗 諛섏씠 �놁뒿�덈떎.")
			.setPositiveButton("�뺤씤", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					// TODO Auto-generated method stub
				}
			}).show();
		}
	}
	void dailyActivity() {
		Intent intent = new Intent(this, DailyActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void answerActivity() {
		Intent intent = new Intent(this, AnswerActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	
	
}



